#include "data_global.h"

extern int fd_sms;
extern struct From_to_send phone_NUM;

void send_message(int fd, char *tel,char *msg);
void call_tel(int fd, char *tel);

void *pthread_sms(void *arg)
{
    printf("pthread_sms\n");
    if((fd_sms = open_port(GPRS_DEV, 1)) == -1)
    {
        perror("sms open");
        // exit(1);
    }
    if (fd_sms != -1 && set_com_config(fd_sms, 9600, 8, 'N', 1) < 0)
    {
        perror("set_com_config");
        //exit(-1);
    }

    if(fd_sms != -1) send_message(fd_sms, phone_NUM.recieve_phone, "hello message!");
    if(fd_sms != -1) call_tel(fd_sms, phone_NUM.recieve_phone);

    if(fd_sms != -1) close(fd_sms);
}

void send_message(int fd, char *tel,char *msg)
{
	printf("enter send_message .....\n");
	int n=0,ret=0,num=0;
	char end=26;
	char buff[128]={0};

	memset(buff,0,128);//测试串口是否正常工作，尽可能的多判断返回值
	strcpy(buff,"AT\n");
	write (fd,buff,strlen(buff));
	read(fd,buff,128);		
	if(strncmp(buff,"OK",2)){
		printf("serial port connected success \n");	
	}else{
		printf("serial port connected failed  \n");
	}
	usleep(10000);		

	memset(buff,0,128);								
	strcpy(buff,"AT+CPIN?\n");//查询是否检测到SIM卡
	write (fd,buff,strlen(buff));          								
	read(fd,buff,128);
	printf("set coding type success,SIM卡成功检测到.\n");				
	usleep(10000);


	memset(buff,0,128);									
	strcpy(buff,"AT+CMGF=1\n");         //设置短信发送模式
	write (fd,buff,strlen(buff));  // CMGF :0(默认)：PDU模式 1：文本模式
	read(fd,buff,128);
	if(strncmp(buff,"OK",2)){
		printf("set message type success \n");
	}else{
		printf("set message type failed\n");
	}				
	usleep(100000);

	memset(buff,0,128);
	strcpy(buff,"AT+CSMP=17,167,0,240\n");         //参数4：241 短信存在sim卡中 240直接在终端显示			
	write (fd,buff,strlen(buff));
	read(fd,buff,128);usleep(10000);
	printf("message save at sim card!\n");
	usleep(100000);

	memset(buff,0,128);
	strcpy(buff,"AT+CMGDA=6\n");        			 //删除之前发送的信息与指令		
	write (fd,buff,strlen(buff));					
	read(fd,buff,128);
	if(strncmp(buff,"OK",2)){
		printf("delect message success!\n");				
	}else{
		printf("delect failed!\n");		
	}						

	memset(buff,0,128);
	sprintf(buff,"AT+CMGS=\"%s\"",tel);  //发送短信
	strcat(buff,"\n");
	write (fd,buff,strlen(buff));
	read(fd,buff,128);
	if(strncmp(buff,">",1))
	{
		if(*msg=='\0')
		{
			printf("please input the message information :");					
			scanf("%s",buff);					
			write(fd,buff,strlen(buff));					
			read(fd,buff,128);	
			write(fd,&end,1);
			read(fd,buff,128);
		}else{
			strcpy(buff, msg);
			write(fd,buff,strlen(buff));					
			read(fd,buff,128);	
			write(fd,&end,1);
			read(fd,buff,128);	
		}

		n=strlen(buff);
		if(strncmp(&buff[n-1],"O",1)&&strncmp(&buff[n],"K",1)){
			printf("send message success :%s\n",msg);
		}else{
			printf("send message failed\n");
		}
		sleep(1);
	}else{
		printf("send message failed!\n");
	}
	sleep(2);
	return ;
}

void call_tel(int fd, char *tel)
{
	printf("enter call_tel .....\n");
	int n=0,ret=0,num=0;
	char end=26;
	char buff[128]={0};

	memset(buff,0,128);//测试串口是否正常工作，尽可能的多判断返回值
	strcpy(buff,"AT\n");
	write (fd,buff,strlen(buff));
	read(fd,buff,128);
	if(strncmp(buff,"OK",2)){
		printf("serial port connected success \n");	
	}else{
		printf("serial port connected failed  \n");
	}
	sleep(1);

	printf("tel ......\n");
	memset(buff,0,128);
	sprintf(buff,"ATD%s;",tel);  //打电话
	strcat(buff,"\n");
	printf("buff :%s.\n",buff);
	write (fd,buff,strlen(buff));
	read(fd,buff,128);
	printf("read from gprs 拨打:%s\n",buff);
	
	sleep(15); //15s之后挂断
	printf("挂断.....");
	memset(buff,0,128);//挂断电话
	strcpy(buff,"ATH;");
	strcat(buff,"\n");
	write (fd,buff,strlen(buff));
	read(fd,buff,128);
	printf("read from gprs 挂断:%s\n",buff);

	sleep(15);
	printf("重拨....\n");
	memset(buff,0,128);//测试串口是否正常工作，尽可能的多判断返回值
	strcpy(buff,"ATDL;");
	strcat(buff,"\n");
	write (fd,buff,strlen(buff));
	read(fd,buff,128);
	printf("read from gprs 重拨:%s\n",buff);
	
	sleep(10);
	return ;
}

